﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellScheduledThreadPoolExecutor_h
#define CellScheduledThreadPoolExecutor_h

#include "CellPrerequisites.h"
#include "CellScheduledExecutorService.h"
#include "CellThreadPoolExecutor.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

class ScheduledFutureImpl;
class ScheduledHalfWindowPolicy;

/** 任务表线程池执行器。
@author
	Jiangwei Xu
*/
class _CellExport ScheduledThreadPoolExecutor : public ScheduledExecutorService,
	public ThreadPoolExecutor, public GeneralAlloc
{
public:
	/**
	*/
	ScheduledThreadPoolExecutor(const String& name, uint32 corePoolSize, uint32 maxPoolSize);

	/**
	*/
	virtual ~ScheduledThreadPoolExecutor(void);

public:
	/**
	*/
	ScheduledFuture* schedule(Runnable* task, ulong delay, uint8 unit);

	/**
	*/
	ScheduledFuture* scheduleAtFixedRate(Runnable* task, ulong initialDelay, ulong period, uint8 unit);

	/**
	*/
	ScheduledFuture* scheduleWithFixedDelay(Runnable* task, ulong initialDelay, ulong delay, uint8 unit);

	/**
	*/
	void remove(Runnable* task);

	/**
	*/
	int getTaskCount(void);

	/**
	*/
	void cancel(Runnable* task);

	/**
	*/
	void addListener(ExecutorListener* listener);

	/**
	*/
	void removeListener(ExecutorListener* listener);

protected:
	/**
	*/
	virtual bool execute(Runnable* task);

	/**
	*/
	virtual void beforeExecute(Runnable* task);

	/**
	*/
	virtual void afterExecute(Runnable* task);

	/**
	*/
	bool isCancelled(Runnable* task);

	/**
	*/
	void destroyDesertedFutures(void);

protected:
	typedef HashMap<Runnable*, ScheduledFutureImpl*> ScheduledFutureMap;
	typedef std::vector<ScheduledFutureImpl*> ScheduledFutureList;

	ScheduledFutureMap mScheduledFutures;
	ScheduledFutureList mDesertedFutures;

private:
	ScheduledHalfWindowPolicy* mPolicy;

	CELL_MUTEX(mMutex);

	friend class ScheduledFutureImpl;
	friend class ScheduledHalfWindowPolicy;
};



/** 任务表半窗策略。
@author
	Jiangwei Xu
*/
class _CellPrivate ScheduledHalfWindowPolicy : public Thread
{
public:
	/**
	*/
	ScheduledHalfWindowPolicy(ScheduledThreadPoolExecutor* executor);

	/**
	*/
	virtual ~ScheduledHalfWindowPolicy(void);

	/**
	*/
	virtual void run(void);

public:
	/**
	*/
	void stop(void);

	struct Schedule : public GeneralAlloc
	{
		Schedule(Runnable* task);
		~Schedule(void);

		Runnable* task;						// Task
		uint64 origin;						// Origin time
		uint64 permitTime;					// Task executed time
		uint64 lastBeginTime;
		uint64 lastEndTime;
		bool worked;						// Worked
		bool deserted;						// Deserted
		bool executing;						// Executing
		ulong initialDelayMillis;			// Initial delay in millisecond
		ulong periodMillis;
		ulong delayMillis;
	};

	/**
	*/
	Schedule* addTask(Runnable* task, ulong initialDelay, uint8 unit);

	/**
	*/
	Schedule* addTaskAtFixedRate(Runnable* task, ulong initialDelay, ulong period, uint8 unit);

	/**
	*/
	Schedule* addTaskWithFixedDelay(Runnable* task, ulong initialDelay, ulong delay, uint8 unit);

	/**
	*/
	void removeTask(Runnable* task);

	/**
	*/
	bool hasTask(Runnable* task);

	/**
	*/
	void fireBeforeExecute(Runnable* task);

	/**
	*/
	void fireAfterExecute(Runnable* task);

	typedef HashMap<Runnable*, Schedule*> ScheduleMap;
	typedef std::vector<Schedule*> ScheduleList;

private:
	bool mWorking;
	bool mSpinning;
	uint64 mRollWindowSizeMillis;
	ScheduledThreadPoolExecutor* mExecutor;
	ScheduleMap mSchedules;
	ScheduleList mDesertedSchedules;
	CELL_MUTEX(mMoniter);
};


/** 任务表异步执行结果的实现类。
@author
	Jiangwei Xu
*/
class _CellPrivate ScheduledFutureImpl : public ScheduledFuture,
	public GeneralAlloc
{
public:
	ScheduledFutureImpl(ScheduledThreadPoolExecutor* executor, Runnable* runnable);
	virtual ~ScheduledFutureImpl(void);

public:
	/**
	*/
	ulong getDelay(uint8 unit);

	/**
	*/
	bool cancel(void);

	/**
	*/
	bool isCancelled(void);

private:
	ScheduledThreadPoolExecutor* mExecutor;
	Runnable* mRunnable;

	friend class ScheduledThreadPoolExecutor;
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellScheduledThreadPoolExecutor_h
